import { AnyAction } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message } from 'antd';
import { wxPay } from './service';

const R = require('ramda');

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    submitRegularForm: Effect;
  };
}
const Model: ModelType = {
  namespace: 'payConfirm',

  state: {
    user: {},
  },

  effects: {
    *wxPay({ payload }, { call, put }) {
      const response = yield call(wxPay, { ...R.omit(['onSuccess', 'onFail'])(payload) });
      if (response && response.resultCode === 'ok') {
        var data = response.content;
        if (data != null && data != '' && data != '{}') {
          if (payload.onSuccess) payload.onSuccess(data);
        } else {
          if (payload.onFail) payload.onFail();
        }
      } else {
        if (payload.onFail) payload.onFail();
      }
    },
  },

  reducers: {
    /**
     * 刷新state
     *
     * @param state
     */
    refreshState(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};

export default Model;
